<?php
/**
 * Created by PhpStorm.
 * User: liu
 * Date: 2018/5/4
 * Time: 15:54
 */

namespace app\index\controller\sunfc;

use app\index\controller\Base;
use app\index\controller\Weixin;
use app\index\model\guo\DdUsersBalanceLog;
use think\Db;
use think\helper\Time;

class Users extends Base
{
    /**
     * 查询在个人资料页面中用户的状态值（GET）
     * URL：index/sunfc.users/index
     */
    public function index()
    {
        $result = Db::name('dd_users')->where('uid',$this->uid)->find();
        $qiniuyun = "?imageMogr2/auto-orient/thumbnail/!320x240r/gravity/Center/crop/320x240/quality/100/interlace/0";
        $result['headimgurl'] = $result['headimgurl'].$qiniuyun;
        $this->assign('result',$result);
        // 判断用户今日是否签到
        $signtime = Db::name('dd_users_balance')->where('uid',$this->uid)->value('sign_time');
        list($start, $end) = Time::today();
        $signIn = ($signtime<$start)?0:1;
        $this->assign('signIn',$signIn);
        return $this->fetch();
    }
    /**
     * 签到（GET）
     * URL：index/sunfc.users/signIn
     */
    public function signIn()
    {
            // 判断用户今日是否签到
            $signtime = Db::name('dd_users_balance')->where('uid',$this->uid)->value('sign_time');
            list($start, $end) = Time::today();
            if ($signtime<$start){
                $data = input();
                // 查询每日签到的分值
                $sign = Db::name('option')->where('option_name','point_set')->value('option_value');
                $sign_in=json_decode($sign, true);
                $data['number'] = $sign_in['signed'];
                $data['create_time'] = time();
                $data['uid'] = $this->uid;
                // 调用签到接口
                $log_model = new DdUsersBalanceLog();
                if($log_model->insert_log($data)){
                   $this->success('签到成功!');
                }else{
                    $this->error('签到失败!');
                }
            }else{
                // 今日已签到
                $this->error('今日已签到!');
            }


    }
    /**
     * 资料编辑（GET）
     * URL：index/sunfc.users/messEdit
     */
    public function messedit()
    {
        if(request()->isAjax()){
            $data = input();
            $data['update_time'] = time();
            // 将用户提交的个人资料放入数据库中
            $result = Db::name('dd_users')->where('uid',$this->uid)->update($data);
            if($result){
                return $this->success("资料更新成功");
            }else{
                return $this->error("资料更新失败");
            }

        }else{

            $this->assign("uid",$this->uid);
        }

        $result = Db::name('dd_users')->where('uid',$this->uid)->find();
        $qiniuyun = "?imageMogr2/auto-orient/thumbnail/!320x240r/gravity/Center/crop/320x240/quality/100/interlace/0";
        $result['avatar'] = $result['headimgurl'].$qiniuyun;
        $this->assign('result',$result);
        return $this->fetch();
    }
    /**
     * 实名认证编辑（GET）
     * URL：index/sunfc.users/authUser
     */
    public function authuser()
    {
        if(request()->isAjax()){
            $data = input();

            $data['update_time'] = time();
            $data['auth_user'] = 1;
            // 判断手机验证码
            $res = Db::name('sms_code')
                ->where('phone',$data['tel'])
                ->where('code',$data['password'])
                ->find();
            if (!$res){
                $this->error('手机验证码有误');
            }
            // 将用户提交的个人信息放入数据库中
            unset($data['password']);
            $result = Db::name('dd_users')->where('uid',$data['uid'])->update($data);
            if($result){
                return json(['code' => 1, 'msg' => '信息提交成功!']);
            }else{
                return json(['code' => 200, 'msg' => '信息提交失败!']);
            }

        }else{
            $result = Db::name('dd_users')->where('uid',$this->uid)->find();
            $this->assign("res",$result);
            $this->assign("uid",$this->uid);
            return $this->fetch();
        }

    }
    /**
     * 验证手机号码是否注册过（GET）
     * URL：index/sunfc.users/checkphone
     */
    public function checkphone()
    {
        $data = input();
        $result = Db::name('dd_users')
            ->where('tel',$data['phone'])
            ->select();
        $number = count($result);
        if($number>1){
            //手机号码已被别人占用
            return $this->error("手机号码已占用");

        }elseif ($number=1){
            // 判断是用户本人认证未通过
            $res =  Db::name('dd_users')->where('uid',$this->uid)->value('auth_user');
            if ($res == -1){
                //  未通过认证
                return $this->success();
            }elseif ($res == 0){
                return $this->success();
            }
            else{
                return $this->error("手机号码已注册过");
            }
        } else{
            return $this->success();
        }

    }


    /**
     * 验证身份证有效性
     */
    public function checkIdCard()
    {
        $id_card = request()->post('id_card');
        if(!$id_card){
            $this->error("请填写正确的身份证号");
        }

        // 验证
        $res = check_id_card($id_card);
        if($res){
            return $this->success("身份证有效");
        }else{
            return $this->error("请填写正确的身份证号");
        }

    }
    /**
     * 司机认证编辑（GET）
     * URL：index/sunfc.users/authDriver
     */
    public function authdriver()
    {
        if(request()->isAjax()){
            $data = input();

            $data['update_time'] = time();
            $data['auth_driver'] = 1;
            // 将用户提交的个人信息放入数据库中
            $result = Db::name('dd_users')->where('uid',$this->uid)->update($data);
            if($result){
                return $this->success("信息提交成功");
            }else{
                return $this->error("信息提交失败");
            }

        }else{
            $this->assign("uid",$this->uid);
            $res = Db::name('dd_users')->where('uid',$this->uid)->find();
            $qiniuyun = "?imageMogr2/auto-orient/thumbnail/!320x240r/gravity/Center/crop/320x240/quality/100/interlace/0";
            $res['driver_license'] = $res['driver_license'].$qiniuyun;
            $res['vehicle_license'] = $res['vehicle_license'].$qiniuyun;
            $this->assign("res",$res);
            return $this->fetch();
        }

    }
    /**
     * 司机信息查看（GET）
     * URL：index/sunfc.users/authDriverLook
     */
    public function authDriverLook()
    {
        $res = Db::name('dd_users')->where('uid',$this->uid)->find();
        $this->assign("res",$res);
        return $this->fetch();
    }
}